﻿using LaserAutoTest.Worker;
using LaserAutoTest.Worker.Jobs;
using Microsoft.Extensions.DependencyInjection;
using Prism.DryIoc;
using Prism.Ioc;
using Quartz.Impl;
using Quartz.Spi;
using System;

namespace LaserAutoTest.UI.Extensions
{
    public static class JobExtension
    {
        public static void AddJobs(this IServiceCollection services)
        {
            //services.AddSingleton<HCJob>();
            //services.AddSingleton<IJobFactory, JobFactory>();
            services.Scan(scan =>
            {
                scan.FromAssemblyOf<JobFactory>()
                    .AddClasses(classes => classes.Where(t => t.Name.EndsWith("Job", StringComparison.OrdinalIgnoreCase)))
                    .AsSelf()
                    .WithSingletonLifetime();
            });
            services.AddSingleton<IJobFactory, JobFactory>();
        }

        public static async void RegisterJobsAsync(this IContainerRegistry containerRegistry)
        {
            var scheduler = await StdSchedulerFactory.GetDefaultScheduler();
            scheduler.JobFactory = containerRegistry.GetContainer().GetService<IJobFactory>(); // 获取并设置自定义作业工厂  
            await scheduler.Start();
            await scheduler.RegisterJobs();
        }
       
    }
}
